package com.ymatou.cs.api.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.cs.api.service.CreateReturnApplyBillCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.AgreeApplyWapper;
import com.ymttest.database.sqlwapper.CreateReturnApplyBillWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*CreateReturnApplyBill
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CreateReturnApplyBill{
	private static CreateReturnApplyBillCall createreturnapplybillCall;
	String RefundBillNo =null; //退单号
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("CreateReturnApplyBill");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createreturnapplybillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public static String chackreturncode(JSONObject dataStr) throws Exception {
	    try {	    	
			//返回报文断言		
			Logger.verifyNotNull(dataStr, "返回报文非空");
			//断言"Status":200
			String Status = dataStr.getString("Status");
			Logger.verifyIsContains(Status, 200, "\"Code\":200");
			//断言"Msg":""
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsNull(msg, "Msg等于空");
			//断言"bCode":""
			String bCode =  dataStr.getString("bCode");
			Logger.verifyIsContains(bCode, 0, "\"bCode\":0");
		
			
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	    return "";
	}
	
	
	//创建退货退款TOken
	public void Toke(){
		
		Logger.debug("************************创建Token************************");
		createreturnapplybillCall = new CreateReturnApplyBillCall("lunchzhao", "123456");
	}
		
	 
	 
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateReturnApplyBill_001() {
		Logger.start(true,"买家 创建部分仅退款");
		//  int iOrderId ;
		 // String sCatalogId;
		try {
			
			//获取买家ToKen
			
				Toke();
			
			//通过查询的Token反查买家的用户ID 
			
			//获取订单号  规格  					
			CreateReturnApplyBillWapper createReturnApplyBillWapper = new CreateReturnApplyBillWapper();
	    	Map m = new HashMap();
	        List<Map> rm = createReturnApplyBillWapper.ioderidCatLogs();
	        int  iOrderId = (Integer) rm.get(0).get("iOrderId");
	        String  sCatalogId=(String)rm.get(0).get("sCatalogId");
	         
	        
	        //创建退货退款	
	    	Logger.debug("************************买家 创建部分仅退款************************");
			List<String> PicUrls=new ArrayList<>();
			PicUrls.add("http://pm2.img.ymatou.com/G01/upload/product/list/M00/07/14/rBBlD1YuBKKAJhsFAAZi-BfcdCc770_l.jpg");			
			Map<String, Object> map = new HashMap<String, Object>();		
			map.put("OrderId",iOrderId);			    
			map.put("ReturnAmount",0.01);
			map.put("ReturnRemark","niras通过api创建的退款单-djj");
			map.put("ReturnReasonId",12);
			map.put("PicUrls",new JSONArray(PicUrls));
			map.put("products[0]->CatalogId",sCatalogId);
			map.put("products[0]->ReturnCount",1);

			//调用接口json文件
			JSONObject createreturnapplybill =  JsonBeanHelper.getJsonBean("/com.ymatou.cs.api.ReturnApplyBill/CreateReturnApplyBill/CreateReturnApplyBilltpl.json");
			createreturnapplybill = JsonBeanHelper.updateJsonBean(createreturnapplybill, map); 		
			JSONObject result = createreturnapplybillCall.callAndGetReturnData(createreturnapplybill);
 
			//Logger.debug(result);
			//创建成功检查退单号是否正确,返回正常结果后查询出退单信息
			String dataStr=createreturnapplybillCall.getReturnData();				
			String Status =  new JSONObject(dataStr).getString("Status");
			String bCode =  new JSONObject(dataStr).getString("bCode");				
			if(Status=="200" && bCode=="0")
			{
				Map mapRefundBil = new HashMap();	    	
				mapRefundBil.put("OrderId", iOrderId);
		    	List<Map> rmmapRefundBil = createReturnApplyBillWapper.GetRefundBil(mapRefundBil);
		    	for(Map RefundBil : rmmapRefundBil){
		    		
		    	    RefundBillNo = String.valueOf(map.get("RefundBillNo"));
		    	    String RefundOrderId = String.valueOf(map.get("OrderId"));
//		        	Logger.debug(RefundBillNo);  
//		        	Logger.debug(RefundOrderId);  
		    	}    
			}	    	 
			//断言校验
			chackreturncode(result);
			String  ReturnId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("ReturnId"); 	
			String  ROrderId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("OrderId"); 	
		//	Logger.verifyIsNull(ReturnId, "ReturnId等于空");
			Logger.verifyEquals(iOrderId,Integer.parseInt(ROrderId), "请求订单号对比返回订单号 ！");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
//	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateReturnApplyBill_002() {
		Logger.start(true,"买家  创建全额仅退款");
		//  int iOrderId ;
		 // String sCatalogId;
		try {
			
			//获取买家ToKen
			Toke();
			
			//获取订单号  规格  					
			CreateReturnApplyBillWapper createReturnApplyBillWapper = new CreateReturnApplyBillWapper();
	    	Map m = new HashMap();
	        List<Map> rm = createReturnApplyBillWapper.ioderidCatLogs();
	        int  iOrderId = (Integer) rm.get(0).get("iOrderId");   //订单
	        String  sCatalogId=(String)rm.get(0).get("sCatalogId");//规格
	        BigDecimal  fPayableAmount=(BigDecimal)rm.get(0).get("fPayableAmount");//退款金额
	        
	       Logger.debug("***---------------/////////"+fPayableAmount);
	        
	        //创建退货退款	
	    	Logger.debug("************************买家  创建全额仅退款 ************************");
			List<String> PicUrls=new ArrayList<>();
			PicUrls.add("http://pm2.img.ymatou.com/G01/upload/product/list/M00/07/14/rBBlD1YuBKKAJhsFAAZi-BfcdCc770_l.jpg");			
			Map<String, Object> map = new HashMap<String, Object>();		
			map.put("OrderId",iOrderId);			    
			map.put("ReturnAmount",fPayableAmount);
			map.put("ReturnRemark","niras通过api创建的退款单-djj");
			map.put("ReturnReasonId",12);
			map.put("PicUrls",new JSONArray(PicUrls));
			map.put("products[0]->CatalogId",sCatalogId);
			map.put("products[0]->ReturnCount",1);

			//调用接口json文件
			JSONObject createreturnapplybill =  JsonBeanHelper.getJsonBean("/com.ymatou.cs.api.ReturnApplyBill/CreateReturnApplyBill/CreateReturnApplyBilltpl.json");
			createreturnapplybill = JsonBeanHelper.updateJsonBean(createreturnapplybill, map); 		
			JSONObject result = createreturnapplybillCall.callAndGetReturnData(createreturnapplybill);
 
			//Logger.debug(result);
			//创建成功检查退单号是否正确,返回正常结果后查询出退单信息
			String dataStr=createreturnapplybillCall.getReturnData();				
			String Status =  new JSONObject(dataStr).getString("Status");
			String bCode =  new JSONObject(dataStr).getString("bCode");				
			if(Status=="200" && bCode=="0")
			{
				Map mapRefundBil = new HashMap();	    	
				mapRefundBil.put("OrderId", iOrderId);
		    	List<Map> rmmapRefundBil = createReturnApplyBillWapper.GetRefundBil(mapRefundBil);
		    	for(Map RefundBil : rmmapRefundBil){
		    		
		    	    RefundBillNo = String.valueOf(map.get("RefundBillNo"));
		    	    String RefundOrderId = String.valueOf(map.get("OrderId"));
//		        	Logger.debug(RefundBillNo);  
//		        	Logger.debug(RefundOrderId);  
		    	}    
			}	    	 
			//断言校验
			chackreturncode(result);
			String  ReturnId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("ReturnId"); 	
			String  ROrderId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("OrderId"); 	
		//	Logger.verifyIsNull(ReturnId, "ReturnId等于空");
			Logger.verifyEquals(iOrderId,ROrderId, "请求订单号对比返回订单号 ！");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
//	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateReturnApplyBill_003() {
		Logger.start(true,"买家创建全额退货退款");
		//  int iOrderId ;
		 // String sCatalogId;
		try {
			
			//获取买家ToKen
			Toke();
			
			//获取订单号  规格  					
			CreateReturnApplyBillWapper createReturnApplyBillWapper = new CreateReturnApplyBillWapper();
	    	Map m = new HashMap();
	        List<Map> rm = createReturnApplyBillWapper.tuihuotuikuanRefundBill();
	        int  iOrderId = (Integer) rm.get(0).get("iOrderId");   //订单
	        String  sCatalogId=(String)rm.get(0).get("sCatalogId");//规格
	        BigDecimal  fPayableAmount=(BigDecimal)rm.get(0).get("fPayableAmount");//退款金额
	        
	       Logger.debug("***---------------/////////"+fPayableAmount);
	        
	        //创建退货退款	
	    	Logger.debug("************************买 创建全额退货退款 ************************");
			List<String> PicUrls=new ArrayList<>();
			PicUrls.add("http://pm2.img.ymatou.com/G01/upload/product/list/M00/07/14/rBBlD1YuBKKAJhsFAAZi-BfcdCc770_l.jpg");			
			Map<String, Object> map = new HashMap<String, Object>();		
			map.put("OrderId",iOrderId);			    
			map.put("ReturnAmount",fPayableAmount);
			map.put("ReturnRemark","niras通过api创建的退款单-djj");
			map.put("ReturnReasonId",12);
			map.put("PicUrls",new JSONArray(PicUrls));
			map.put("products[0]->CatalogId",sCatalogId);
			map.put("products[0]->ReturnCount",1);

			//调用接口json文件
			JSONObject createreturnapplybill =  JsonBeanHelper.getJsonBean("/com.ymatou.cs.api.ReturnApplyBill/CreateReturnApplyBill/CreateReturnApplyBilltpl.json");
			createreturnapplybill = JsonBeanHelper.updateJsonBean(createreturnapplybill, map); 		
			JSONObject result = createreturnapplybillCall.callAndGetReturnData(createreturnapplybill);
 
			//Logger.debug(result);
			//创建成功检查退单号是否正确,返回正常结果后查询出退单信息
			String dataStr=createreturnapplybillCall.getReturnData();				
			String Status =  new JSONObject(dataStr).getString("Status");
			String bCode =  new JSONObject(dataStr).getString("bCode");				
			if(Status=="200" && bCode=="0")
			{
				Map mapRefundBil = new HashMap();	    	
				mapRefundBil.put("OrderId", iOrderId);
		    	List<Map> rmmapRefundBil = createReturnApplyBillWapper.GetRefundBil(mapRefundBil);
		    	for(Map RefundBil : rmmapRefundBil){
		    		
		    	    RefundBillNo = String.valueOf(map.get("RefundBillNo"));
		    	    String RefundOrderId = String.valueOf(map.get("OrderId"));

		    	}    
			}	    	 
			//断言校验
			chackreturncode(result);
			String  ReturnId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("ReturnId"); 	
			String  ROrderId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("OrderId"); 	
		//	Logger.verifyIsNull(ReturnId, "ReturnId等于空");
			Logger.verifyEquals(iOrderId,ROrderId, "请求订单号对比返回订单号 ！");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
//	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateReturnApplyBill_004() {
		Logger.start(true,"买家创建全额退货退款,(无效的订单)");
		//  int iOrderId ;
		 // String sCatalogId;
		try {
			
			//获取买家ToKen
			Toke();
			
			//获取订单号  规格  					
			CreateReturnApplyBillWapper createReturnApplyBillWapper = new CreateReturnApplyBillWapper();
	    	Map m = new HashMap();
	        List<Map> rm = createReturnApplyBillWapper.ioderidCatLogs();
	        int  iOrderId = 888888888;  //订单
	        String  sCatalogId=(String)rm.get(0).get("sCatalogId");//规格
	        BigDecimal  fPayableAmount=(BigDecimal)rm.get(0).get("fPayableAmount");//退款金额
	        
	  
	        //创建退货退款	
	    	Logger.debug("************************买 创建全额退货退款 ************************");
			List<String> PicUrls=new ArrayList<>();
			PicUrls.add("http://pm2.img.ymatou.com/G01/upload/product/list/M00/07/14/rBBlD1YuBKKAJhsFAAZi-BfcdCc770_l.jpg");			
			Map<String, Object> map = new HashMap<String, Object>();		
			map.put("OrderId",888888888);			    
			map.put("ReturnAmount",fPayableAmount);
			map.put("ReturnRemark","niras通过api创建的退款单-djj");
			map.put("ReturnReasonId",12);
			map.put("PicUrls",new JSONArray(PicUrls));
			map.put("products[0]->CatalogId",sCatalogId);
			map.put("products[0]->ReturnCount",1);

			//调用接口json文件
			JSONObject createreturnapplybill =  JsonBeanHelper.getJsonBean("/com.ymatou.cs.api.ReturnApplyBill/CreateReturnApplyBill/CreateReturnApplyBilltpl.json");
			createreturnapplybill = JsonBeanHelper.updateJsonBean(createreturnapplybill, map); 		
			JSONObject result = createreturnapplybillCall.callAndGetReturnData(createreturnapplybill);
 
			//Logger.debug(result);
			//创建成功检查退单号是否正确,返回正常结果后查询出退单信息
			String dataStr=createreturnapplybillCall.getReturnData();				
			String Status =  new JSONObject(dataStr).getString("Status");
			String bCode =  new JSONObject(dataStr).getString("bCode");				
			if(Status=="200" && bCode=="0")
			{
				Map mapRefundBil = new HashMap();	    	
				mapRefundBil.put("OrderId", iOrderId);
		    	List<Map> rmmapRefundBil = createReturnApplyBillWapper.GetRefundBil(mapRefundBil);
		    	for(Map RefundBil : rmmapRefundBil){
		    		
		    	    RefundBillNo = String.valueOf(map.get("RefundBillNo"));
		    	    String RefundOrderId = String.valueOf(map.get("OrderId"));

		    	}    
			}	    	 
			//断言校验
			//返回报文断言		
			Logger.verifyNotNull(dataStr, "返回报文非空");
			//断言"Status":200
			String Status1 = result.getString("Status");
			Logger.verifyIsContains(Status1, 200, "\"Code\":200");
			//断言"Msg":""
			String msg =  result.getString("Msg");
			Logger.verifyIsNull(msg, "Msg等于空");
			//断言"bCode":""
			String bCode1 =  result.getString("bCode");
			Logger.verifyIsContains(bCode1, 6, "\"bCode\":6");
			String  ReturnId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("ReturnId"); 	
			String  ROrderId = result.getJSONObject("RetData").getJSONObject("ReturnBill").getString("OrderId"); 	
		//	Logger.verifyIsNull(ReturnId, "ReturnId等于空");
			Logger.verifyNotEquals(iOrderId,ROrderId, "请求订单号对比返回订单号 ！");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}